import axios from 'axios'
import {message as antdMessage} from 'antd'

const MESSAGE = 'MESSAGE'
const FETCHING = 'FETCHING'
const FETCHED = 'FETCHED'
const SAVEING = 'SAVEING'
const SAVEED = 'SAVEED'
const DELETEING = 'DELETEING'
const DELETED = 'DELETED'
const ADDING = 'ADDING'
const ADDED = 'ADDED'

const init = {
    data:[],
    msg:'',
    error:'',
    pagination:{
        total:0
    },
    loading:false,
    saveing:false,
    deleteing:false
}

export function catalog(state=init, action) {
    switch(action.type) {
        case FETCHING:
            return {...state, loading:true}
        case FETCHED:
            return {...state, data:action.data, pagination:{...state.pagination, total:action.total}, loading:false}
        case MESSAGE:
            action.msg && antdMessage.info(action.msg)
            action.error && antdMessage.error(action.error)
            return {...state, msg:action.msg, error:action.error, loading:false, saveing:false}
        case SAVEING:
            return {...state, saveing:true}
        case SAVEED:
            const updated = state.data.map(v=>{
                if (v.number === action.params.number) {
                    return action.params
                }
                return v
            })
            antdMessage.info('保存成功')
            return {...state, saveing:false, data:updated, msg: '保存成功'}
        case DELETEING:
            return {...state, deleteing:true}
        case DELETED:
            antdMessage.info('删除成功')
            return {...state, deleteing:false, data:state.data.filter(v=>v.number!=action.number)}
        case ADDING:
            return {...state, saveing:true}
        case ADDED:
            antdMessage.info('新增成功')
            return {...state, data:[...state.data, action.data], saveing:false}
        default:
            return state
    }
}

function message(msg) {
    return {
        type: MESSAGE,
        msg
    }
}

function error(error) {
    return {
        type: MESSAGE,
        error
    }
}

function fetching() {
    return {
        type:FETCHING
    }
}

function fetched(data, total) {
    return {
        type:FETCHED,data,total
    }
}

export function fetch(params) {
    return dispatch=>{
        dispatch(fetching())
        axios.post('/catalog/list', {'params':params}).then(res=>{
            if (res.status == 200) {
                if (res.data.code == 1) {
                    let result = res.data.data.data
                    result = result.map(v=>{ return {...v, key:v.id}})
                    dispatch(fetched(result, res.data.data.total))
                } else {
                    dispatch(error(res.data.msg))
                }
            } else {
                dispatch(error('网络异常:'+res.status))
            }
        })
    }
}

function saveing() {
    return {
        type: SAVEING
    }
}

function saveed(params) {
    return {
        type: SAVEED,params
    }
}

export function save(params) {
    if (!params['number']) {
        return message('缺少必须参数number');
    }
    return dispatch=>{
        dispatch(saveing())
        axios.post('/catalog/update', params).then(res=>{
            if (res.status == 200) {
                if (res.data.code == 1) {
                    dispatch(saveed(params))
                } else {
                    dispatch(error(res.data.msg ? res.data.msg : '保存失败'))
                }
            } else {
                dispatch(error('网络异常:'+res.status))
            }
        })
    }
}

function deleteing() {
    return {
        type: DELETEING
    }
}

function deleted(number) {
    return {
        type: DELETED,
        number
    }
}

export function del(number) {
    return dispatch=>{
        dispatch(deleteing())
        axios.post('/catalog/del', {number}).then(res=>{
            if (res.status == 200) {
                if (res.data.code == 1) {
                    dispatch(deleted(number))
                } else {
                    dispatch(error(res.data.msg ? res.data.msg : '删除失败'))
                }
            } else {
                dispatch(error('网络异常:'+res.status))
            }
        })
    }
}

function adding() {
    return {
        type: ADDING
    }
}

function added(data) {
    return {
        type: ADDED,
        data
    }
}

export function add(params) {
    return dispatch=>{
        dispatch(adding())
        axios.post('/catalog/add', params).then(res=>{
            if (res.status == 200) {
                if (res.data.code == 1) {
                    params['id'] = res.data.data
                    dispatch(added(params))
                } else {
                    dispatch(error(res.data.msg ? res.data.msg : '新增失败'))
                }
            } else {
                dispatch(error('网络异常:'+res.status))
            }
        })
    }
}